package batch.import_user;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.dbutils.*;
import org.apache.log4j.*;

import java.util.ArrayList;
import java.sql.*;
import java.io.*;

import batch.common.db.ConnectionManager;

public class compareImportMember
{
    private final String FILE_NAME = "/home01/sfc/batch/import_user/data/ita_200605_manzoni_dem_imported.txt";
    private final String MEMBERGBN = "10397103";

    //arrStrData[0] : email
    //arrStrData[1] : gender
    //arrStrData[2] : firstname
    //arrStrData[3] : lastname
    //arrStrData[4] : birthday
    //arrStrData[5] : hp_brand

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.compareImportMember").info("( * ) Start.");

        compareImportMember obj = new compareImportMember();
        obj.init();

        Logger.getLogger("import_user.compareImportMember").info("( * ) End.");
    }


    public void init(){
        Connection conn = null;

        BufferedReader bfrdForFile = null;

        String strTemp   = "";

        ArrayList params = new ArrayList();

        StringBuffer strQuery = new StringBuffer()
                               .append("SELECT MEMBERGBN       \n")
                               .append("  FROM G_MB_MEMBER     \n")
                               .append(" WHERE DELETED IS NULL \n")
                               .append("   AND U_EMAIL = ?     \n");

        Logger.getLogger("import_user.compareImportMember").info(FILE_NAME);

        String[][] sMissed = new String[100][2];
        int indexLng = 0;

        try{
            bfrdForFile = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "ISO-8859-1"));

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);

            QueryRunner qRunner = new QueryRunner();

            for(int i=0; (strTemp = bfrdForFile.readLine()) != null; i++) {
                String arrStrData[] = StringUtils.splitPreserveAllTokens(strTemp, "\t");

                params.clear();
                params.add(arrStrData[0].toLowerCase());  //EMAIL

                ResultSetHandler rsh = new ScalarHandler(1);

                String sRE = StringUtils.defaultString((String)qRunner.query(conn, strQuery.toString(), params.toArray(), rsh));

                if(!sRE.equals(MEMBERGBN)){
                    sMissed[indexLng][0] = arrStrData[0];
                    sMissed[indexLng][1] = sRE;

                    indexLng++;
                }

                if(i % 500 == 0){
                    Logger.getLogger("import_user.compareImportMember").info("here we are == " + i);
                }
            }

            for(int i=0;i<indexLng;i++){
                Logger.getLogger("import_user.compareImportMember").info("Missed == [" + i + "] == " + sMissed[i][0]);
                Logger.getLogger("import_user.compareImportMember").info("Missed == [" + i + "] == " + sMissed[i][1]);
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
            Logger.getLogger("import_user.compareImportMember").info(ex.toString());
        }
        finally {
            if (bfrdForFile != null) try { bfrdForFile.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }
}
